package my.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Customer;
import my.dao.ICustomerDao;

public class ICustomerDaoImpl implements ICustomerDao {
	private Connection conn;

	public ICustomerDaoImpl() {

	}

	public ICustomerDaoImpl(Connection conn) {
		this.conn = conn;
	}

	public void setConnection(Connection conn) {
		this.conn = conn;
	}

	@Override
	public int add(Customer customer) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;

		try {
			String sql = "insert into customer values(user_id.nextval,?,?,?,?,?)";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, customer.getAccount());
			prestmt.setString(2, customer.getPassword());
			prestmt.setString(3, customer.getEmail());
			prestmt.setString(4, customer.getPhone());
			prestmt.setTimestamp(5, new Timestamp(customer.getRegDate().getTime()));

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("注册成功,请登录!");
			} else {
				System.out.println("注册失败,请重新注册");
			}
			return record;
		} catch (SQLException e) {
			System.out.println("插入失败,请重新操作,谢谢.");
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int deleteById(int userid) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;
		try {
			String sql = "delete from customer where user_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, userid);

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!请重新操作");
			}

			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}

	}

	@Override
	public int modify(Customer customer) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;
		try {
			String sql = "update Customer set user_account=?,user_email=?,user_phone=? where user_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, customer.getAccount());
			prestmt.setString(2, customer.getEmail());
			prestmt.setString(3, customer.getPhone());
			prestmt.setInt(4, customer.getUserId());

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("修改成功!");
			} else {
				System.out.println("修改失败!请重新操作!");
			}

			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}

	}

	@Override
	public List<Customer> findAll() {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		List<Customer> list = new ArrayList<>();

		try {
			String sql = "select * from customer";
			prestmt = conn.prepareStatement(sql);

			rs = prestmt.executeQuery();

			while (rs.next()) {
				int cid = rs.getInt(1);
				String account = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);

				Customer customer = new Customer(cid, account, password, email, phone, regDate);
				list.add(customer);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return list;
	}

	@Override
	public Customer findById(int useriId) {
		PreparedStatement prestmt = null;
		Customer customer = null;
		ResultSet rs = null;
		try {
			String sql = "select * from customer where user_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, useriId);

			rs = prestmt.executeQuery();

			if (rs.next()) {
				int cid = rs.getInt(1);
				String account = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);

				customer = new Customer(cid, account, password, email, phone, regDate);
			}

			return customer;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return customer;
	}

	@Override
	public Customer findByUserAcount(String accout) {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		Customer customer = null;
		try {
			String sql = "select * from customer where user_account=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, accout);

			rs = prestmt.executeQuery();
			if (rs.next()) {
				int cid = rs.getInt(1);
				String account = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);

				customer = new Customer(cid, account, password, email, phone, regDate);
			}
			return customer;

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return customer;
	}

	@Override
	public int modifyPassword(Customer customer) throws SQLException {
		PreparedStatement prestmt = null;
		int record = 0;

		try {
			String sql = "update customer set password=? where user_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, customer.getPassword());
			prestmt.setInt(2, customer.getUserId());

			record = prestmt.executeUpdate();

			if (record > 0) {
				System.out.println("密码修改成功!");
			} else {
				System.out.println("密码修改失败!请重新操作!");
			}

			return record;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public Customer findByAccountAndPassword(String account, String password) {
		PreparedStatement prestmt = null;
		ResultSet rs = null;
		Customer customer = null;

		try {
			String sql = "select * from customer where user_account=? and trim(user_password)=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, account);
			prestmt.setString(2, password);

			rs = prestmt.executeQuery();
			if (rs.next()) {
				int cid = rs.getInt(1);
				account = rs.getString(2);
				password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);

				customer = new Customer(cid, account, password, email, phone, regDate);
			}
			return customer;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, null);
		}
		return customer;
	}

}
